package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> pageInfo1 = new Page<>();

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(name != null, Dish::getName,name);
        qw.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,qw);

        BeanUtils.copyProperties(pageInfo,pageInfo1,"records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list = records.stream().map((item) ->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId();
            Category dish = categoryService.getById(categoryId);
            if (dish != null) {
                String name1 = dish.getName();
                dishDto.setCategoryName(name1);
            }
            return dishDto;
        }).collect(Collectors.toList());

        pageInfo1.setRecords(list);

        return R.success(pageInfo1);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){

        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);

        return R.success("修改菜品成功");
    }

    @DeleteMapping
    public R<String> delete(Long[] ids){
        dishService.deleteWithFlavorBatch(ids);

        return R.success("成功删除");
    }

    @PostMapping("/status/{status}")
    public R<List<Dish>> updateStatus(@PathVariable Integer status, @RequestParam("ids") Long[] id){

        ArrayList<Dish> list = new ArrayList<>();
        for (Long item : id) {
            Dish dish = dishService.getById(item);
            dish.setStatus(status);
            list.add(dish);
        }

        dishService.updateBatchById(list);
        return R.success(list);
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        qw.eq(Dish::getStatus,1);
        qw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(qw);

        List<DishDto> listDto = new ArrayList<>();
        for (Dish dish0 : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish0,dishDto);
            LambdaQueryWrapper<DishFlavor> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(DishFlavor::getDishId,dish0.getId());
            List<DishFlavor> list1 = dishFlavorService.list(qw1);

            dishDto.setFlavors(list1);
            listDto.add(dishDto);
        }
        return R.success(listDto);

    }


}
